package com.uottawa.librarytest.service;


import static org.junit.Assert.*;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.uottawa.library.dao.BaseDAO;
import com.uottawa.library.dao.UserDAO;
import com.uottawa.library.domain.Item;
import com.uottawa.library.domain.LoanPeriod;
import com.uottawa.library.domain.TransactionLogger;
import com.uottawa.library.domain.User;
import com.uottawa.library.service.LoanPeriodService;
import com.uottawa.library.service.LoanService;
import com.uottawa.library.service.SystemMonitoringService;
import com.uottawa.library.service.UserService;

public class SystemMonitoringTest {
	
	private SystemMonitoringService monitoringService;
	private UserService userService;
	private LoanService loanService;
	
	
	@Before
	public void setUp() throws Exception {
		monitoringService = new SystemMonitoringService();
		userService = new UserService();
		loanService = new LoanService();
		
		LoanPeriodService loanPeriodService = new LoanPeriodService();
		LoanPeriod loanPeriod = new LoanPeriod(1,5);  //initial loan period duration 
		User adminUser = new User(1, "admin", "0000", 0, "admin");
		new UserDAO().addUser(adminUser);
		
		loanPeriodService.addPeriod(loanPeriod, adminUser);

	}

	@After
	public void tearDown() throws Exception {
		new BaseDAO().cleanUp();
	}

	@Test
	public void testMonitorAddUser() {
		// assert the number of transactions before creating a user
		List<TransactionLogger> logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(0, logs.size());
		
		User user = new User(1111, "OkUser", "password", 0, "user");
		userService.addUser(user);
		
		// assert that one entry was logged in DB.
		logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(1, logs.size());
		
		// assert the correct action is logged.
		TransactionLogger log = logs.get(0);
		assertEquals("addUser", log.getActionName());
		assertEquals(1111, log.getUserId());
		assertNotNull(log.getActionDate());
		
	}
	
	@Test
	public void testMonitorDeleteUser() {
		// assert the number of transactions before creating a user
		List<TransactionLogger> logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(0, logs.size());
		
		User user =  new User(1111, "OkUser", "password", 0, "user");;
		userService.addUser(user);
		userService.deleteUser(user);
		
		// assert that two entry was logged in DB. ( insert and remove)
		logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(2, logs.size());
		
		// assert the correct action is logged for the second action.
		TransactionLogger log = logs.get(1);
		assertEquals("deleteUser", log.getActionName());
		assertEquals(1111, log.getUserId());
		assertNotNull(log.getActionDate());
		
	}
	
	
	@Test
	public void testMonitorCreateLoan() {
		// assert the number of transactions before creating a user
		List<TransactionLogger> logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(0, logs.size());
				
		Item item = new Item();
		item.setItemId(55555);
		
		User user = new User();
		user.setUserId(1111);
		userService.addUser(user);

		loanService.borrowLoanCopy(item, user);
		// assert that one entry was logged in DB
		logs = monitoringService.getTransactionsByUserId(1111);
		assertEquals(2, logs.size());
		
		// assert the correct action is logged (second log)
		TransactionLogger log = logs.get(1);
		assertEquals("borrowLoanCopy", log.getActionName());
		assertEquals(1111, log.getUserId());
		assertNotNull(log.getActionDate());
		
	}
}
